
/*

make a booth that is 3x3, with counters
players can place items on the counters for sale

*/

mob/keepers
	name = "Shopkeeper"
	base_shopkeeper
		DblClick()
			speak_local(src,"Hey there. Welcome to my humble shop built into a rock. \
			Please sell me things! If you put things in my sell box, it makes me some food, and I really want some food. I can't get any\
			, because I'm trapped behind my box. It was a bad move, you see, but I'm doing what I can..")

obj/shop_related
	display_items
		mouse_opacity=2
		New()
			..()
			spawn(20)world.log << src
			for(var/mob/keepers/o in orange(15, src))
				if(o)
					keep_ref = o
					break
		var
			cost
			newitem_path
			mob/keep_ref
			stackable
			stack_size

		DblClick()
			var/mob/player/m = usr
			if(get_dist(src, m) > 2) return

			if(m.jewels < cost)
				if(keep_ref)speak_local(keep_ref, "Hey, you don't have enough jewels for that! You need [cost]!")
				else m << "Hey, you don't have enough jewels for that! You need [cost]!"
				return

			var/total_cost = cost

			var/total_amount

			if(stackable)

				var/x = input(m, "How many [src] would you like to buy? Max: [min(round(m.jewels/cost), stack_size)]") as num

				x = safe_num(x)
				if(x<=0) x = 1
				if(x>stack_size) x = stack_size

				if((x * cost) > m.jewels)
					m << "You don't have enough jewels for that!"
					return

				total_amount = x
				total_cost = x*cost

			if(alert(m, "Would you like to buy [src][stackable?" (x[total_amount])":""], costing [total_cost] jewels?","Buying [src]","Yes","No")=="Yes")
				m.jewels -= total_cost
				var/obj/gettable/o = new newitem_path
				if(stackable) o.amount = total_amount
				o.Move(m)
				if(keep_ref)speak_local(keep_ref, "Thanks for your purchase, [m]!")
				else m << "Thanks for your purchase, [m]!"


	sell_box
		//drag and drop selling, gets value
		icon='objects.dmi'
		icon_state="sellbox"
		density=1

		proc/sell(mob/player/m, obj/gettable/s)

			if(m.client.busy) return
			m.client.busy = 1

			var/obj/gettable/stackable/crushobj
			var/gain=0

			if(s.amount>1)
				var/x = input(m, "How many would you like to sell? Max: [s.amount], Value [s.val] per","Selling [s]") as num
				x = min(abs(round(x)),s.amount)

				if(x==s.amount)
					s.Move(loc)
					crushobj = s

				else if(x>=1)
					crushobj = new s.type(loc, x)
					var/obj/gettable/stackable/y = s
					y.remove(x)

				gain = x*s.val
			else
				s.Move(loc)
				crushobj=s
				gain = s.val

			m << "You got [gain] jewels!"

			m.jewels += gain

			m.update_contents()

			crush(crushobj)

			m.client.busy = 0

		proc/crush(obj/o)
			spawn(25)
				if(o)
					oview(10,src) << "The mechanical crunching machine crunches [o] to dust!"
					del o